package edu.szcu.vitaguard.dao;

import edu.szcu.vitaguard.entity.VisitRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 就诊记录DAO接口
 */
@Mapper
public interface VisitRecordDao {
    
    /**
     * 插入就诊记录
     * @param visitRecord 就诊记录对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO visit_record(reg_id, patient_id, doctor_id, visit_date, " +
            "diagnosis, treatment_plan, create_time, update_time) " +
            "VALUES(#{regId}, #{patientId}, #{doctorId}, #{visitDate}, " +
            "#{diagnosis}, #{treatmentPlan}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "visitId")
    int insert(VisitRecord visitRecord);
    
    /**
     * 根据ID查询就诊记录
     * @param visitId 就诊ID
     * @return 就诊记录对象
     */
    @Select("SELECT v.*, p.patient_name, p.gender, p.birth, p.phone, p.id_card, " +
            "d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "r.reg_date, r.start_time, r.end_time, r.reg_type, r.status as reg_status " +
            "FROM visit_record v " +
            "LEFT JOIN patient p ON v.patient_id = p.patient_id " +
            "LEFT JOIN doctor d ON v.doctor_id = d.doctor_id " +
            "LEFT JOIN registration r ON v.reg_id = r.reg_id " +
            "WHERE v.visit_id = #{visitId}")
    @Results({
        @Result(property = "visitId", column = "visit_id"),
        @Result(property = "regId", column = "reg_id"),
        @Result(property = "patientId", column = "patient_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "visitDate", column = "visit_date"),
        @Result(property = "diagnosis", column = "diagnosis"),
        @Result(property = "treatmentPlan", column = "treatment_plan"),
        @Result(property = "patient", column = "patient_id", javaType = edu.szcu.vitaguard.entity.Patient.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.PatientDao.selectById")),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "registration", column = "reg_id", javaType = edu.szcu.vitaguard.entity.Registration.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.RegistrationDao.selectById"))
    })
    VisitRecord selectById(Integer visitId);
    
    /**
     * 查询所有就诊记录
     * @return 就诊记录列表
     */
    @Select("SELECT v.*, p.patient_name, p.gender, p.birth, p.phone, p.id_card, " +
            "d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "r.reg_date, r.start_time, r.end_time, r.reg_type, r.status as reg_status " +
            "FROM visit_record v " +
            "LEFT JOIN patient p ON v.patient_id = p.patient_id " +
            "LEFT JOIN doctor d ON v.doctor_id = d.doctor_id " +
            "LEFT JOIN registration r ON v.reg_id = r.reg_id")
    @Results({
        @Result(property = "visitId", column = "visit_id"),
        @Result(property = "regId", column = "reg_id"),
        @Result(property = "patientId", column = "patient_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "visitDate", column = "visit_date"),
        @Result(property = "diagnosis", column = "diagnosis"),
        @Result(property = "treatmentPlan", column = "treatment_plan"),
        @Result(property = "patient", column = "patient_id", javaType = edu.szcu.vitaguard.entity.Patient.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.PatientDao.selectById")),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "registration", column = "reg_id", javaType = edu.szcu.vitaguard.entity.Registration.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.RegistrationDao.selectById"))
    })
    List<VisitRecord> selectAll();
    
    /**
     * 根据患者ID查询就诊记录
     * @param patientId 患者ID
     * @return 就诊记录列表
     */
    @Select("SELECT v.*, p.patient_name, p.gender, p.birth, p.phone, p.id_card, " +
            "d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "r.reg_date, r.start_time, r.end_time, r.reg_type, r.status as reg_status " +
            "FROM visit_record v " +
            "LEFT JOIN patient p ON v.patient_id = p.patient_id " +
            "LEFT JOIN doctor d ON v.doctor_id = d.doctor_id " +
            "LEFT JOIN registration r ON v.reg_id = r.reg_id " +
            "WHERE v.patient_id = #{patientId}")
    @Results({
        @Result(property = "visitId", column = "visit_id"),
        @Result(property = "regId", column = "reg_id"),
        @Result(property = "patientId", column = "patient_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "visitDate", column = "visit_date"),
        @Result(property = "diagnosis", column = "diagnosis"),
        @Result(property = "treatmentPlan", column = "treatment_plan"),
        @Result(property = "patient", column = "patient_id", javaType = edu.szcu.vitaguard.entity.Patient.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.PatientDao.selectById")),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "registration", column = "reg_id", javaType = edu.szcu.vitaguard.entity.Registration.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.RegistrationDao.selectById"))
    })
    List<VisitRecord> selectByPatientId(Integer patientId);
    
    /**
     * 根据医生ID查询就诊记录
     * @param doctorId 医生ID
     * @return 就诊记录列表
     */
    @Select("SELECT v.*, p.patient_name, p.gender, p.birth, p.phone, p.id_card, " +
            "d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "r.reg_date, r.start_time, r.end_time, r.reg_type, r.status as reg_status " +
            "FROM visit_record v " +
            "LEFT JOIN patient p ON v.patient_id = p.patient_id " +
            "LEFT JOIN doctor d ON v.doctor_id = d.doctor_id " +
            "LEFT JOIN registration r ON v.reg_id = r.reg_id " +
            "WHERE v.doctor_id = #{doctorId}")
    @Results({
        @Result(property = "visitId", column = "visit_id"),
        @Result(property = "regId", column = "reg_id"),
        @Result(property = "patientId", column = "patient_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "visitDate", column = "visit_date"),
        @Result(property = "diagnosis", column = "diagnosis"),
        @Result(property = "treatmentPlan", column = "treatment_plan"),
        @Result(property = "patient", column = "patient_id", javaType = edu.szcu.vitaguard.entity.Patient.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.PatientDao.selectById")),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "registration", column = "reg_id", javaType = edu.szcu.vitaguard.entity.Registration.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.RegistrationDao.selectById"))
    })
    List<VisitRecord> selectByDoctorId(Integer doctorId);
    
    /**
     * 根据预约ID查询就诊记录
     * @param regId 预约ID
     * @return 就诊记录对象
     */
    @Select("SELECT v.*, p.patient_name, p.gender, p.birth, p.phone, p.id_card, " +
            "d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "r.reg_date, r.start_time, r.end_time, r.reg_type, r.status as reg_status " +
            "FROM visit_record v " +
            "LEFT JOIN patient p ON v.patient_id = p.patient_id " +
            "LEFT JOIN doctor d ON v.doctor_id = d.doctor_id " +
            "LEFT JOIN registration r ON v.reg_id = r.reg_id " +
            "WHERE v.reg_id = #{regId}")
    @Results({
        @Result(property = "visitId", column = "visit_id"),
        @Result(property = "regId", column = "reg_id"),
        @Result(property = "patientId", column = "patient_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "visitDate", column = "visit_date"),
        @Result(property = "diagnosis", column = "diagnosis"),
        @Result(property = "treatmentPlan", column = "treatment_plan"),
        @Result(property = "patient", column = "patient_id", javaType = edu.szcu.vitaguard.entity.Patient.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.PatientDao.selectById")),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "registration", column = "reg_id", javaType = edu.szcu.vitaguard.entity.Registration.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.RegistrationDao.selectById"))
    })
    VisitRecord selectByRegId(Integer regId);
    
    /**
     * 根据就诊日期范围查询就诊记录
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 就诊记录列表
     */
    @Select("SELECT v.*, p.patient_name, p.gender, p.birth, p.phone, p.id_card, " +
            "d.doctor_name, d.title, d.introduction, d.status as doctor_status, " +
            "r.reg_date, r.start_time, r.end_time, r.reg_type, r.status as reg_status " +
            "FROM visit_record v " +
            "LEFT JOIN patient p ON v.patient_id = p.patient_id " +
            "LEFT JOIN doctor d ON v.doctor_id = d.doctor_id " +
            "LEFT JOIN registration r ON v.reg_id = r.reg_id " +
            "WHERE v.visit_date BETWEEN #{startDate} AND #{endDate}")
    @Results({
        @Result(property = "visitId", column = "visit_id"),
        @Result(property = "regId", column = "reg_id"),
        @Result(property = "patientId", column = "patient_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "visitDate", column = "visit_date"),
        @Result(property = "diagnosis", column = "diagnosis"),
        @Result(property = "treatmentPlan", column = "treatment_plan"),
        @Result(property = "patient", column = "patient_id", javaType = edu.szcu.vitaguard.entity.Patient.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.PatientDao.selectById")),
        @Result(property = "doctor", column = "doctor_id", javaType = edu.szcu.vitaguard.entity.Doctor.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.DoctorDao.selectById")),
        @Result(property = "registration", column = "reg_id", javaType = edu.szcu.vitaguard.entity.Registration.class, 
                one = @One(select = "edu.szcu.vitaguard.dao.RegistrationDao.selectById"))
    })
    List<VisitRecord> selectByDateRange(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
    
    /**
     * 更新就诊记录
     * @param visitRecord 就诊记录对象
     * @return 影响的行数
     */
    @Update("UPDATE visit_record SET reg_id = #{regId}, patient_id = #{patientId}, " +
            "doctor_id = #{doctorId}, visit_date = #{visitDate}, diagnosis = #{diagnosis}, " +
            "treatment_plan = #{treatmentPlan}, update_time = #{updateTime} " +
            "WHERE visit_id = #{visitId}")
    int update(VisitRecord visitRecord);
    
    /**
     * 删除就诊记录
     * @param visitId 就诊ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM visit_record WHERE visit_id = #{visitId}")
    int deleteById(Integer visitId);
}